package com.fengmi.search.mq;

import com.fengmi.goods.MallGoods;
import com.fengmi.search.bean.ESGoods;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RocketMQMessageListener(consumerGroup = "fengmi-goods-search-consumer",
        topic = "goods-search",selectorExpression = "audit",
        consumeMode = ConsumeMode.CONCURRENTLY,
        messageModel = MessageModel.CLUSTERING)
public class SyncES implements RocketMQListener<MallGoods> {
    @Autowired
    ElasticsearchRestTemplate template;
    @Override
    public void onMessage(MallGoods mallGoods) {
        //把 mallGoods 转化成 ESGoods类型即可
        ESGoods eg = new ESGoods();
        eg.setSpuId(mallGoods.getSpuId());
        eg.setGoodsName(mallGoods.getGoodsName());
        eg.setBrandId(mallGoods.getBrand().getId());//注意不要直接getBrandID
        eg.setBrandName(mallGoods.getBrand().getName());
        eg.setCid1id(mallGoods.getCat1().getId());
        eg.setCat1name(mallGoods.getCat1().getName());
        eg.setCid2id(mallGoods.getCat2().getId());
        eg.setCat2name(mallGoods.getCat2().getName());
        eg.setCid3id(mallGoods.getCat3().getId());
        eg.setCat3name(mallGoods.getCat3().getName());
        eg.setCreateTime(new Date());
        eg.setPrice(Double.valueOf(mallGoods.getPrice().toString()));
        eg.setImageUrl(mallGoods.getAlbumPics());
        template.save(eg);
        System.out.println("同步数据完成"+eg.getSpuId());
    }
    //mq 将模块之间进行解耦
}
